package com.kaiwukj.android.ufamily.mvp.ui.page.service.property.notice;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.BaseSwipeBackFragment;
import com.kaiwukj.android.ufamily.di.component.DaggerServiceComponent;
import com.kaiwukj.android.ufamily.di.module.ServiceModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommunityNoticeResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.ServiceContract;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Copyright © KaiWu Technology Company
 * 公告列表
 *
 * @author Haife
 * @job Android Development
 */
public class CommunityNoticeFragment extends BaseSwipeBackFragment<CommunityNoticePresenter> implements ServiceContract.CommunityNoticeView {
    @BindView(R.id.qtb_community_notice)
    QMUITopBar mQMUITopBar;

    @BindView(R.id.rv_community_notice)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;


    private MyNoticeListAdapter mNoticeListAdapter;
    private int pageNum = 1;
    private int pageSize = 12;
    private boolean isLoadingMore = false;

    public static CommunityNoticeFragment newInstance() {
        CommunityNoticeFragment fragment = new CommunityNoticeFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerServiceComponent.builder()
                .appComponent(appComponent)
                .serviceModule(new ServiceModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_community_notice;
    }

    @Override
    protected void initView() {
        mQMUITopBar.setTitle("物业公告");
        mQMUITopBar.addLeftBackImageButton().setOnClickListener(view -> {
            if (getActivity() != null) {
                getActivity().onBackPressed();
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(10));
        mNoticeListAdapter = new MyNoticeListAdapter();
        mRecyclerView.setAdapter(mNoticeListAdapter);
        //mNoticeListAdapter.setEmptyView(, mRecyclerView);
        mNoticeListAdapter.setEmptyView(R.layout.empty_layout_my_notice_list);
        mNoticeListAdapter.setOnItemClickListener((adapter, view, position) ->
                start(CommunityNoticeDetailFragment.newInstance(mNoticeListAdapter.getItem(position))));

        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadingMore = true;
                mPresenter.fetchNoticeList(++pageNum, pageSize);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.fetchNoticeList(pageNum = 1, pageSize);
            }
        });
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        assert mPresenter != null;
        mPresenter.fetchNoticeList(pageNum, pageSize);
        showLoading();

    }

    @Override
    public void onFetchResult(List<CommunityNoticeResult> results) {
        hideLoading();
        if (isLoadingMore) {
            isLoadingMore = false;
            mNoticeListAdapter.addData(results);
            if (results.size() < pageSize) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                mRefreshLayout.finishLoadMore(true);
            }
        } else {
            mRefreshLayout.finishRefresh(true);
            mNoticeListAdapter.setList(results);
        }
    }

    @Override
    public void onGetResult(CommunityNoticeResult result) {

    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        hideLoading();
        mRefreshLayout.finishRefresh(false);
        mRefreshLayout.finishLoadMore(false);
    }

    static class MyNoticeListAdapter extends BaseQuickAdapter<CommunityNoticeResult, BaseViewHolder> {

        /**
         * Same as QuickAdapter#QuickAdapter(Context,int) but with
         * some initialization data.
         */
        public MyNoticeListAdapter() {
            super(R.layout.recycle_item_community_notice, new ArrayList<>());
            addChildClickViewIds(R.id.tv_community_notice_detail);
        }

        /**
         * Implement this method and use the helper to adapt the view to the given item.
         *
         * @param helper A fully initialized helper.
         * @param item   The item that needs to be displayed.
         */
        @Override
        protected void convert(BaseViewHolder helper, CommunityNoticeResult item) {
            helper.setText(R.id.tv_community_notice_time, item.getCreateTime())
                    .setText(R.id.tv_community_notice_title, item.getTitle())
                    .setText(R.id.tv_community_notice_content, item.getNote());
        }


    }
}
